package logic

import (
	"context"
	"fmt"
	"project/model/mysql"

	"project/projectrpc/internal/svc"
	"project/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserListLogic) UserList(in *projectrpc.UserListRequest) (*projectrpc.UserListResponse, error) {
	// todo: add your logic here and delete this line
	var myself []mysql.Myself

	var count int64

	mysql.DB.Model(&mysql.Myself{}).Count(&count)

	if in.Page < 1 {
		in.Page = 1
	}

	offset := (in.Page - 1) * in.PageSize

	mysql.DB.Offset(int(offset)).Limit(int(in.PageSize)).Find(&myself)

	var sli []*projectrpc.Myself
	for _, i2 := range myself {
		one := projectrpc.Myself{
			Id:       int64(i2.ID),
			Username: i2.Username,
			Tel:      i2.Tel,
			Email:    i2.Email,
			Price:    float32(i2.Price),
			Start:    fmt.Sprintf("%v", i2.CreatedAt),
		}
		sli = append(sli, &one)
	}

	return &projectrpc.UserListResponse{
		Sum:    count,
		Myself: sli,
	}, nil
}
